﻿using AutoMapper;
using MediatR;
using RedDragon.Common.Domain.AuthorizeRBAC;
using RedDragon.Common.Infrastructure.Interface;
using RedDragon.RBAC.API.Application.Common.Permission;
using RedDragon.RBAC.ErrorCodeEnum;
using T.Common.Domain.Common;

namespace RedDragon.RBAC.API.Application.Common.Resourec
{
    public class AddResourecHandler : IRequestHandler<AddResourecCommand, Result>
    {
        /// <summary>
        /// mapper
        /// </summary>
        private readonly IMapper mapper;
        /// <summary>
        /// 权限仓储
        /// </summary>
        public readonly IBaseRepository<Resource> resourceRepository;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="mapper"></param>
        /// <param name="resourceRepository"></param>
        public AddResourecHandler(IMapper mapper, IBaseRepository<Resource> resourceRepository)
        {
            this.mapper = mapper;
            this.resourceRepository = resourceRepository;
        }
        /// <summary>
        /// 资源添加
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>

        public async Task<Result> Handle(AddResourecCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Added };
            Resource Resource = mapper.Map<Resource>(request);
            var res = await resourceRepository.Add(Resource);
            if (res > 0)
            {
                result.Code = (int)CommonErrorCode.Success;
                result.Msg = "资源添加成功";
            }
            return result;
        }
    }
}
